﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IOPath = System.IO.Path; // 为 System.IO.Path 设置一个别名
using System.IO;
using System.Windows;

namespace SimpleFileExplorer
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadDirectories();
            filesListView.MouseDoubleClick += FilesListView_MouseDoubleClick;
        }

        private void LoadDirectories()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
            foldersTreeView.Items.Add(CreateDirectoryNode(dirInfo));
        }

        private TreeViewItem CreateDirectoryNode(DirectoryInfo directoryInfo)
        {
            TreeViewItem directoryNode = new TreeViewItem { Header = directoryInfo.Name, Tag = directoryInfo };
            try
            {
                foreach (var directory in directoryInfo.GetDirectories())
                {
                    directoryNode.Items.Add(CreateDirectoryNode(directory));
                }
            }
            catch (UnauthorizedAccessException)
            {
                // 可以在这里记录错误，或者向用户显示一条消息
                // 例如：directoryNode.Items.Add(new TreeViewItem { Header = "Access Denied" });
            }
            directoryNode.Selected += DirectoryNode_Selected;
            return directoryNode;
        }


        private void DirectoryNode_Selected(object sender, RoutedEventArgs e)
        {
            var treeViewItem = sender as TreeViewItem;
            if (treeViewItem == null || treeViewItem.Tag == null)
                return;

            var directoryInfo = treeViewItem.Tag as DirectoryInfo;
            filesListView.Items.Clear();
            try
            {
                foreach (var dir in directoryInfo.GetDirectories())
                    filesListView.Items.Add(dir.Name);
                foreach (var file in directoryInfo.GetFiles())
                    filesListView.Items.Add(file.Name);
            }
            catch { }
        }
        private void FilesListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            

            if (filesListView.SelectedItem == null)
                return;

            string selectedFile = filesListView.SelectedItem.ToString();
            string fullPath = IOPath.Combine((foldersTreeView.SelectedItem as TreeViewItem)?.Tag.ToString() ?? "", selectedFile);

            if (File.Exists(fullPath) && IOPath.GetExtension(fullPath).Equals(".txt", StringComparison.OrdinalIgnoreCase))
            {
                System.Diagnostics.Process.Start("notepad.exe", fullPath);
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {

        }
    }
}
